package com.epf.sso.sample.config;

import com.epf.sso.core.conf.Conf;
import com.epf.sso.core.filter.EpfSsoWebFilter;
import com.epf.sso.core.util.JedisUtil;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author epf 2019-11-15
 */
@Configuration
public class EpfSsoConfig implements DisposableBean {


    @Value("${epf.sso.server}")
    private String epfSsoServer;

    @Value("${epf.sso.logout.path}")
    private String epfSsoLogoutPath;

    @Value("${epf-sso.excluded.paths}")
    private String epfSsoExcludedPaths;

    @Value("${epf.sso.redis.address}")
    private String epfSsoRedisAddress;


    @Bean
    public FilterRegistrationBean epfSsoFilterRegistration() {

        // epf-sso, redis init
        JedisUtil.init(epfSsoRedisAddress);

        // epf-sso, filter init
        FilterRegistrationBean registration = new FilterRegistrationBean();

        registration.setName("EpfSsoWebFilter");
        registration.setOrder(1);
        registration.addUrlPatterns("/*");
        registration.setFilter(new EpfSsoWebFilter());
        registration.addInitParameter(Conf.SSO_SERVER, epfSsoServer);
        registration.addInitParameter(Conf.SSO_LOGOUT_PATH, epfSsoLogoutPath);
        registration.addInitParameter(Conf.SSO_EXCLUDED_PATHS, epfSsoExcludedPaths);

        return registration;
    }

    @Override
    public void destroy() throws Exception {

        // epf-sso, redis close
        JedisUtil.close();
    }

}
